package com.alks.yn_zs.service.serviceImpl;

import com.alks.common.aop.AutoPageAop;
import com.alks.entity.data.entity.PcSoleStatus;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.yn_zs.data.dto.LastStkDto;
import com.alks.yn_zs.data.dto.StatsDto;
import com.alks.yn_zs.mapper.LastShowMapper;
import com.alks.yn_zs.service.ILastShowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/12/20下午4:57
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class LastShowServiceImpl implements ILastShowService {
    private final LastShowMapper lastShowMapper;

    @Override
    public Map<String, List<StatsDto>> deptStatus(String deptName) {
        List<StatsDto> dtoList = new ArrayList<>();
//        /*获取投产源数据*/
//        List<StatsDto> dtoList = lastShowMapper.deptStatus(deptName);
        /*补充产出源数据*/
        List<StatsDto> dto2List = lastShowMapper.deptStatus2(deptName);
        dtoList.addAll(dto2List);
        /*补充入库数据*/
        List<StatsDto> dto3List = lastShowMapper.deptStatus3(deptName);
        dtoList.addAll(dto3List);
        /*根据TYPE进行MAP分组*/
        Map<String, List<StatsDto>> dtoMap = dtoList.stream().collect(Collectors.groupingBy(StatsDto::getType));
//        /*通过投产-产出获得在制品数据*/
//        Map<String,StatsDto> newDtos = new HashMap<>(dtoList.size());
//        dtoMap.forEach((k, v) -> {
//            BigDecimal inQty = v.stream().filter(a -> "投产".equals(a.getTitle())).map(StatsDto::getCount)
//                    .reduce(BigDecimal.ZERO, BigDecimal::add);
//            BigDecimal outQty = v.stream().filter(a -> "产出".equals(a.getTitle())).map(StatsDto::getCount)
//                    .reduce(BigDecimal.ZERO, BigDecimal::add);
//            BigDecimal qty = inQty.subtract(outQty);
//            qty = qty.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : qty;
//            StatsDto dto = new StatsDto(null, qty, "在制品", k);
//            newDtos.put(k,dto);
//        });
//        dtoMap.values().forEach(list -> list.add(newDtos.get(list.get(0).getType())));
        return dtoMap;
    }

    @Override
    public List<StatsDto> stkStatus() {
        String sole = "1";
        String vamp = "2";
        /*获取基础数据*/
        List<StatsDto> dtoList = new ArrayList<>();
        List<PcSoleStatus> staList = lastShowMapper.stkStatus();
        if (staList==null){
            return dtoList;
        }
        /*拆分鞋面和大底*/
        List<String> lotSizeList = staList.stream().map(a -> a.getLotNo() + "_" + a.getSizeNo()).distinct().collect(Collectors.toList());
        Map<String, BigDecimal> soleLotSizeStk = staList.stream()
                .filter(s -> sole.equals(s.getPartType()))
                .collect(Collectors.toMap(a -> a.getLotNo() + "_" + a.getSizeNo(),
                        a -> a.getInQty().subtract(a.getOutQty()),
                        BigDecimal::add));
        Map<String, BigDecimal> vampLotSizeStk = staList.stream()
                .filter(s -> vamp.equals(s.getPartType()))
                .collect(Collectors.toMap(a -> a.getLotNo() + "_" + a.getSizeNo(),
                        a -> a.getInQty().subtract(a.getOutQty()),
                        BigDecimal::add));
        /*获取配套库存*/
        BigDecimal matchStk = BigDecimal.ZERO;
        for (String s : lotSizeList) {
            BigDecimal soleQty = soleLotSizeStk.get(s) == null ? BigDecimal.ZERO : soleLotSizeStk.get(s);
            BigDecimal vampQty = vampLotSizeStk.get(s) == null ? BigDecimal.ZERO : vampLotSizeStk.get(s);
            BigDecimal matchQty = soleQty.compareTo(vampQty) > 0 ? vampQty : soleQty;
            matchStk = matchStk.add(matchQty);
        }
        /*分别其他库存*/
        BigDecimal soleStk = soleLotSizeStk.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal vampStk = vampLotSizeStk.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        /*整合为输出数据*/
        dtoList.add(new StatsDto(null,matchStk,"配套库存","配套库存"));
        dtoList.add(new StatsDto(null,soleStk,"大底库存","大底库存"));
        dtoList.add(new StatsDto(null,vampStk,"鞋面库存","鞋面库存"));
        return dtoList;
    }

    @Override
    @AutoPageAop
    public PageRecord<LastStkDto> stkDetail(PageParam request, Integer inFlag, Integer stkFlag) {
        return new PageRecord<>(lastShowMapper.stkDetail(inFlag,stkFlag));
    }
}
